<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQueryByMaterialName">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="物料代码">
              <a-input placeholder="请输入物料代码" v-model="queryParam.materialCode"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="物料名称">
              <a-input placeholder="请输入物料名称" v-model="queryParam.materialName"></a-input>
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button type="primary" @click="searchQueryByMaterialName" icon="search">查询</a-button>
      <a-button type="primary" @click="searchReset" icon="reload">重置</a-button>
      <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>
      <a-button type="primary" icon="download" @click="handleExportXls('金蝶BOM')">导出</a-button>
      <a-upload
        name="file"
        :showUploadList="false"
        :multiple="false"
        :headers="tokenHeader"
        :action="importExcelUrl"
        @change="handleImportExcel"
      >
        <a-button type="primary" icon="import">导入</a-button>
      </a-upload>
      <!-- 高级查询区域 -->
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel">
            <a-icon type="delete" />
            删除
          </a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px">
          批量操作
          <a-icon type="down" />
        </a-button>
      </a-dropdown>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择
        <a style="font-weight: 600">{{ selectedRowKeys.length }}</a
        >项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        :scroll="{ x: true }"
        bordered
        rowKey="id"
        v-if="false"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange, type: 'radio' }"
        class="j-table-force-nowrap"
        @change="handleTableChange"
      >
      </a-table>
    </div>

    <hf-bom-from-jindie-modal ref="modalForm" @ok="modalFormOk"></hf-bom-from-jindie-modal>

    <a-card v-for="(item, index) in cardDataSource" style="margin-bottom: 16px;" :key="index">
      <template v-slot:title>
        <a-statistic :title="index + 1" :value="item.find(i => i.orderCode === 0).materialName" />
      </template>
      <a-table ref="table" size="middle" :scroll="{ x: true }" :columns="columns" :dataSource="item" bordered>
      </a-table>
    </a-card>
  </a-card>
</template>

<script>
import '@/assets/less/TableExpand.less'
import { mixinDevice } from '@/utils/mixin'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import HfBomFromJindieModal from './modules/HfBomFromJindieModal'
import { getAction } from '@api/manage'

export default {
  name: 'HfBomFromJindieList',
  mixins: [JeecgListMixin, mixinDevice],
  components: {
    HfBomFromJindieModal
  },
  data() {
    return {
      description: '金蝶BOM管理页面',
      // 表头
      columns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          customRender: function(t, r, index) {
            return parseInt(index) + 1
          }
        },
        {
          title: '组别代码',
          align: 'center',
          dataIndex: 'groupCode'
        },
        {
          title: 'BOM单号',
          align: 'center',
          dataIndex: 'bomCode'
        },
        {
          title: '顺序号',
          align: 'center',
          dataIndex: 'orderCode'
        },
        {
          title: '物料代码',
          align: 'center',
          dataIndex: 'materialCode'
        },
        {
          title: '物料名称',
          align: 'center',
          dataIndex: 'materialName'
        },
        {
          title: '规格型号',
          align: 'center',
          dataIndex: 'sap'
        },
        {
          title: '物料类型',
          align: 'center',
          dataIndex: 'materialType'
        },
        {
          title: '单位',
          align: 'center',
          dataIndex: 'unit'
        },
        {
          title: '用量',
          align: 'center',
          dataIndex: 'standardUsage'
        },
        {
          title: '损耗率',
          align: 'center',
          dataIndex: 'wastage'
        },
        {
          title: '使用状态',
          align: 'center',
          dataIndex: 'isUse'
        },
        {
          title: '发料仓库',
          align: 'center',
          dataIndex: 'warehouse'
        },
        {
          title: '备注',
          align: 'center',
          dataIndex: 'remark'
        },
        {
          title: '建立人员',
          align: 'center',
          dataIndex: 'operator'
        },
        {
          title: '建立日期',
          align: 'center',
          dataIndex: 'madeDate'
        },
        {
          title: '审核状态',
          align: 'center',
          dataIndex: 'isCheck'
        }
      ],
      url: {
        list: '/production/hfBomFromJindie/list',
        cardList: '/production/hfBomFromJindie/queryByMaterialName',
        delete: '/production/hfBomFromJindie/delete',
        deleteBatch: '/production/hfBomFromJindie/deleteBatch',
        exportXlsUrl: '/production/hfBomFromJindie/exportXls',
        importExcelUrl: 'production/hfBomFromJindie/importExcel'
      },
      dictOptions: {},
      superFieldList: [],
      cardDataSource: []
    }
  },
  created() {
    this.getSuperFieldList()
  },
  computed: {
    importExcelUrl: function() {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    }
  },
  methods: {
    searchQueryByMaterialName() {
      this.loading = true
      let param = {
        materialName: this.queryParam.materialName
      }
      getAction(this.url.cardList, param)
        .then(res => {
          if (res.success) {
            this.cardDataSource = res.result.records
          } else {
            this.$message.warning(res.message)
          }
        })
        .finally(() => {
          this.loading = false
        })
    },
    getSuperFieldList() {
      let fieldList = []
      fieldList.push({ type: 'string', value: 'groupCode', text: '组别代码', dictCode: '' })
      fieldList.push({ type: 'string', value: 'bomCode', text: 'BOM单号', dictCode: '' })
      fieldList.push({ type: 'int', value: 'orderCode', text: '顺序号', dictCode: '' })
      fieldList.push({ type: 'string', value: 'materialCode', text: '物料代码', dictCode: '' })
      fieldList.push({ type: 'string', value: 'materialName', text: '物料名称', dictCode: '' })
      fieldList.push({ type: 'string', value: 'sap', text: '规格型号', dictCode: '' })
      fieldList.push({ type: 'string', value: 'materialType', text: '物料类型', dictCode: '' })
      fieldList.push({ type: 'string', value: 'unit', text: '单位', dictCode: '' })
      fieldList.push({ type: 'double', value: 'standardUsage', text: '用量', dictCode: '' })
      fieldList.push({ type: 'double', value: 'wastage', text: '损耗率', dictCode: '' })
      fieldList.push({ type: 'string', value: 'isUse', text: '使用状态', dictCode: '' })
      fieldList.push({ type: 'string', value: 'warehouse', text: '发料仓库', dictCode: '' })
      fieldList.push({ type: 'string', value: 'remark', text: '备注', dictCode: '' })
      fieldList.push({ type: 'string', value: 'operator', text: '建立人员', dictCode: '' })
      fieldList.push({ type: 'string', value: 'madeDate', text: '建立日期', dictCode: '' })
      fieldList.push({ type: 'string', value: 'isCheck', text: '审核状态', dictCode: '' })
      this.superFieldList = fieldList
    }
  }
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>
